// module路由
// @system SHOP-YUN <shop-yun.com>
// @version shop-yun 1.0
// @author xiaotangren  <unphp@qq.com>
// @data 2014-07-21
package taobao

import (
	"fmt"
	//base "shop-yun.go/go-crawl/utils/base"
	conf "shop-yun.go/go-crawl/utils/conf"
	//"strings"
	//"strconv"
)

//socket接口
type moduleController interface {
	Route(action string, params interface{}) (s conf.SendData)
}

func addRouterController(name string, c func() moduleController) {
	ControllerMap[name] = c
}

type Router struct {
}

// 路由：对module（controller）的路由分发控制
func (r *Router) Route(module, table, action string, params interface{}) (rs conf.SendData) {
	defer func() {
		if err := recover(); err != nil {
			rs.Code = 0
			rs.Error = "Error!A fatal error throw by databases module route!"
			fmt.Println(err)
			return
		}
	}()
	var c moduleController
	switch module {
	case "common": //数据表操作“通用接口”
		a := new(Controller)
		c = a
	default:
		if a, ok := ControllerMap[module]; ok {
			c = a()
		} else {
			rs.Code = 0
			rs.Error = "Error::not this api's module![" + module + "]"
			return
		}
	}
	rs = c.Route(action, params)
	return
}
